package com.health.work.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.health.work.R;
import com.health.work.activity.CaseStorageActivity.ViewHolder;
import com.health.work.model.CaseBean;
import com.health.work.utils.AsyncImageLoader;
import com.health.work.utils.DefaultJSONData;
import com.health.work.utils.HEALTHPASSPORT;
import com.health.work.utils.RequestParamtersUtil;
import com.health.work.utils.TipsUtil;
import com.health.work.utils.UserInfoUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
/**
 * 
 * @author yx
 *病例
 */
public class Cases extends Activity{

	private Thread thread;
	private final static int SUCCESS = 0;
	private final static int FAILE = 1;
	private final static int ERROR = 2;
	private final static int NODATA = 3;
	private String mobilePhoneNum;
	private String isOpen;
	private TextView back;
	private GridView grid;
	private TextView nodata;
	private ArrayList<CaseBean>caseList;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SUCCESS:
				proAdapter adapter=new proAdapter(Cases.this);
				grid.setAdapter(adapter);
				break;
			case FAILE:
				Toast.makeText(Cases.this, "请求失败",
						Toast.LENGTH_SHORT).show();
				break;
			case ERROR:
				Toast.makeText(Cases.this, "未知错误",
						Toast.LENGTH_SHORT).show();
				break;
			case NODATA:
				grid.setVisibility(View.GONE);
				nodata.setVisibility(View.VISIBLE);
				break;
			default:
				break;
			}

		}

	};
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.case_layout);
		initPages();
		GetMedicalRecord();
	}

	private void initPages() {
		// TODO Auto-generated method stub
		Bundle bundle = getIntent().getExtras();
		if(bundle!=null){
			mobilePhoneNum=(String)bundle.getString("mobileNum");
		}
		nodata=(TextView)findViewById(R.id.nodata);
		grid=(GridView)findViewById(R.id.brand_grid);
		grid.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				String picName=caseList.get(arg2).Photos;
				Intent intent;
				intent=new Intent(Cases.this, BigImageActivity.class);
				intent.putExtra("picName", picName);
				startActivity(intent);
			}
		});
		back=(TextView)findViewById(R.id.btnBack);
		back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}
	private void GetMedicalRecord() {
		// TODO Auto-generated method stub
		thread=new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				new RequestParamtersUtil().GetMedicalRecord(mobilePhoneNum, "true", new DefaultJSONData() {
					
					@Override
					public void parse(String result) throws JSONException {
						// TODO Auto-generated method stub
						if ("0".equals(result)) {
							handler.sendEmptyMessage(NODATA);
						} 
					}
					
					@Override
					public void parse(JSONObject object) throws JSONException {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void parse(JSONArray array) throws JSONException {
						// TODO Auto-generated method stub
						if(array.toString()!=null&&array.length()>0){
							caseList=new ArrayList<CaseBean>();
							for (int i = 0; i < array.length(); i++) {
								CaseBean bean=new CaseBean();
								bean.Id=array.getJSONObject(i).getInt("Id");
								bean.MobilePhone=array.getJSONObject(i).getString("MobilePhone");
								bean.MedicalName=array.getJSONObject(i).getString("MedicalName");
								bean.Photos=array.getJSONObject(i).getString("Photos");
								bean.Remark=array.getJSONObject(i).getString("Remark");
								bean.Date=array.getJSONObject(i).getString("Date");
								bean.IsOpen=array.getJSONObject(i).getBoolean("IsOpen");
								caseList.add(bean);
							}
							handler.sendEmptyMessage(SUCCESS);
						}
					}
				});
			}
		});
		thread.start();
	}
	
	private class proAdapter extends BaseAdapter {

		private LayoutInflater inflater;
		private AsyncImageLoader asyncImageLoader;
		Activity context;

		public proAdapter(Context context) {
			this.context = (Activity) context;
			asyncImageLoader = new AsyncImageLoader();
			inflater = this.context.getLayoutInflater();
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return caseList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return caseList.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder viewHodler;
			if (convertView == null) {
				viewHodler = new ViewHolder();
				convertView = inflater.inflate(R.layout.grid_item,
						parent, false);
				viewHodler.headImageView=(ImageView)convertView.findViewById(R.id.brand_grid_Image);
				viewHodler.nameTextView=(TextView)convertView.findViewById(R.id.brand_grid_Text);
			} else {
				viewHodler = (ViewHolder) convertView.getTag();
			}
			CaseBean bean=caseList.get(position);
			if (bean.Photos != null) {
				Drawable drawable = asyncImageLoader
						.loadImageFromUrl(bean.Photos);
				if (drawable != null) {
					viewHodler.headImageView.setImageDrawable(drawable);
				} else {
					viewHodler.headImageView
							.setImageResource(R.drawable.nophoto);
				}
			}
			viewHodler.nameTextView.setText(bean.MedicalName);
			convertView.setTag(viewHodler);
			return convertView;
		}

	}
	class ViewHolder {
		public ImageView headImageView;
		public TextView nameTextView;
	}
}
